package com.yinhai.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.yinhai.cfg.AppConfig;
import com.yinhai.constant.Contact;

public class SystemSetupActivity extends Activity implements OnClickListener {
	ImageView backIv;
	private RadioGroup genderGroup = null;
	private RadioButton rb_setup_no, rb_setup_yes;
	EditText hidden_et;
	LinearLayout set_ll;
	TextView tv1, tv2, tv3, tv4, check_text, msg_modifypwd;
	private static ImageView mImageView1 = null;
	private static ImageView mImageView2 = null;
	private static ImageView mImageView3 = null;
	private static ImageView mImageView4 = null;
	private static ImageView mImageView5 = null;
	private static ImageView mImageView6 = null;
	private static ImageView mImageView7 = null;
	private static ImageView mImageView8 = null;
	private static ImageView mImageView9 = null;
	private static ImageView mImageView0 = null;
	private static ImageView mImageViewNo = null;
	private static ImageView mImageViewStar = null;
	String pwdstr = "";
	private ImageView img ;
	private Context ctx;
	private String name ="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.systemsetup);
		ctx = this;
		genderGroup = (RadioGroup) findViewById(R.id.protectRG);
		rb_setup_no = (RadioButton) findViewById(R.id.protectRG_no);
		rb_setup_yes = (RadioButton) findViewById(R.id.protectRG_yes);
		set_ll = (LinearLayout) findViewById(R.id.setup_layout);
		hidden_et = (EditText) findViewById(R.id.hidden_et);

		tv1 = (TextView) findViewById(R.id.pwd1);
		tv2 = (TextView) findViewById(R.id.pwd2);
		tv3 = (TextView) findViewById(R.id.pwd3);
		tv4 = (TextView) findViewById(R.id.pwd4);

		check_text = (TextView) findViewById(R.id.check_text);
		msg_modifypwd = (TextView) findViewById(R.id.msg_modifypwd);
		
		//img = (ImageView)findViewById(R.id.btn_back);
		
//		img.setOnClickListener(new OnClickListener() {
//            
//            @Override
//            public void onClick(View arg0) {
//                if(name.equals("")){
//                    finish();
//                }else{
//                    Intent mainIntent = new Intent(ctx, MainActivity.class);
//                    ctx.startActivity(mainIntent);
//                    finish();
//                }
//                
//            }
//        });
		
		
		msg_modifypwd.setOnClickListener(this);

		// 拨号盘数字
		mImageView1 = (ImageView) findViewById(R.id.nums_one);
		mImageView1.setOnClickListener(this);
		mImageView2 = (ImageView) findViewById(R.id.nums_two);
		mImageView2.setOnClickListener(this);
		mImageView3 = (ImageView) findViewById(R.id.nums_three);
		mImageView3.setOnClickListener(this);
		mImageView4 = (ImageView) findViewById(R.id.nums_four);
		mImageView4.setOnClickListener(this);
		mImageView5 = (ImageView) findViewById(R.id.nums_five);
		mImageView5.setOnClickListener(this);
		mImageView6 = (ImageView) findViewById(R.id.nums_six);
		mImageView6.setOnClickListener(this);
		mImageView7 = (ImageView) findViewById(R.id.nums_seven);
		mImageView7.setOnClickListener(this);
		mImageView8 = (ImageView) findViewById(R.id.nums_eight);
		mImageView8.setOnClickListener(this);
		mImageView9 = (ImageView) findViewById(R.id.nums_nine);
		mImageView9.setOnClickListener(this);
		mImageView0 = (ImageView) findViewById(R.id.nums_zero);
		mImageView0.setOnClickListener(this);
		mImageViewNo = (ImageView) findViewById(R.id.nums_no);
		mImageViewNo.setOnClickListener(this);
		mImageViewStar = (ImageView) findViewById(R.id.nums_star);
		mImageViewStar.setOnClickListener(this);

		SharedPreferences preferences = this.getSharedPreferences(
				AppConfig.LOGIN_CONFIG, Context.MODE_PRIVATE);
		pwdstr = preferences.getString(Contact.LOCAL_LOGIN_PWD, "");
		if (pwdstr.equals("")) {
			rb_setup_no.setChecked(true);
			rb_setup_yes.setChecked(false);
		} else {
			rb_setup_yes.setChecked(true);
			rb_setup_no.setChecked(false);
		}
		backIv = (ImageView) findViewById(R.id.btn_back);
		if(getIntent().hasExtra("Main")){
            name = getIntent().getStringExtra("Main");
            rb_setup_yes.setChecked(true);
            rb_setup_no.setChecked(false);
            backIv.setVisibility(View.GONE);
        }
		
		
		backIv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SharedPreferences preferences = SystemSetupActivity.this
						.getSharedPreferences(AppConfig.LOGIN_CONFIG,
								Context.MODE_PRIVATE);
				String setuppwdstr = preferences.getString(
						Contact.LOCAL_LOGIN_PWD, "");
				if (rb_setup_yes.isChecked() && setuppwdstr.length() < 4) {
					Toast.makeText(SystemSetupActivity.this,
							"请设置4位本机密码，并按下设置按钮！", Toast.LENGTH_SHORT).show();
				} else if(name.equals("")){
                    finish();
                }else{
                    Intent mainIntent = new Intent(ctx, MainActivity.class);
                    ctx.startActivity(mainIntent);
                    finish();
                }
				
				

			}
		});

		if (rb_setup_yes.isChecked()) {
			SharedPreferences preferences1 = SystemSetupActivity.this
					.getSharedPreferences(AppConfig.LOGIN_CONFIG,
							Context.MODE_PRIVATE);
			String setuppwdstr1 = preferences1.getString(
					Contact.LOCAL_LOGIN_PWD, "");
			if (setuppwdstr1.length() == 4) {
				check_text.setText("已启用密码保护");
				msg_modifypwd.setVisibility(View.VISIBLE);
				set_ll.setVisibility(View.GONE);
			} else {
				set_ll.setVisibility(View.VISIBLE);
			}

		}

		hidden_et.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if (this == null) {
					return;
				}
				if (s.length() == 0) {
					tv1.setText("");
					tv2.setText("");
					tv3.setText("");
					tv4.setText("");
					// Juyon
				} else if (s.length() == 1) {
					tv1.setText(s.toString().substring(s.length() - 1,
							s.length()));
					tv2.setText("");
					tv3.setText("");
					tv4.setText("");
				} else if (s.length() == 2) {
					tv2.setText(s.toString().substring(s.length() - 1,
							s.length()));
					tv3.setText("");
					tv4.setText("");
				} else if (s.length() == 3) {
					tv3.setText(s.toString().substring(s.length() - 1,
							s.length()));
					tv4.setText("");
				} else if (s.length() == 4) {
					tv4.setText(s.toString().substring(s.length() - 1,
							s.length()));
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});

		genderGroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						if (checkedId == rb_setup_no.getId()) {
							set_ll.setVisibility(View.GONE);
							SharedPreferences preferences = SystemSetupActivity.this
									.getSharedPreferences(
											AppConfig.LOGIN_CONFIG,
											Context.MODE_PRIVATE);
							Editor ed = preferences.edit();
							ed.putString(Contact.LOCAL_LOGIN_PWD, "");
							ed.commit();
							if(name.equals("")){
			                    //finish();
			                }else{
			                    Intent mainIntent = new Intent(ctx, MainActivity.class);
			                    ctx.startActivity(mainIntent);
			                    finish();
			                }
						} else if (checkedId == rb_setup_yes.getId()) {
							SharedPreferences preferences1 = SystemSetupActivity.this
									.getSharedPreferences(
											AppConfig.LOGIN_CONFIG,
											Context.MODE_PRIVATE);
							String setuppwdstr1 = preferences1.getString(
									Contact.LOCAL_LOGIN_PWD, "");
							if (setuppwdstr1.length() == 4) {
								check_text.setText("已启用密码保护");
								msg_modifypwd.setVisibility(View.VISIBLE);
								set_ll.setVisibility(View.GONE);
							} else {
								check_text.setText("启用密码保护（推荐）");
								msg_modifypwd.setVisibility(View.GONE);
								set_ll.setVisibility(View.VISIBLE);
							}
						}
					}
				});

	}

	// 拨数字
	private void addDialNum(char numChar) {
		int length = hidden_et.getText().length();
		if (length < 4) {
			hidden_et.getText().append(numChar);
		}

	}

	// 删除数字
	private void deleteNum() {
		int length = hidden_et.getText().length();
		if (length > 0) {
			hidden_et.getText().delete(length - 1, length);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.nums_one:
			addDialNum('1');
			break;
		case R.id.nums_two:
			addDialNum('2');
			break;
		case R.id.nums_three:
			addDialNum('3');
			break;
		case R.id.nums_four:
			addDialNum('4');
			break;
		case R.id.nums_five:
			addDialNum('5');
			break;
		case R.id.nums_six:
			addDialNum('6');
			break;
		case R.id.nums_seven:
			addDialNum('7');
			break;
		case R.id.nums_eight:
			addDialNum('8');
			break;
		case R.id.nums_nine:
			addDialNum('9');
			break;
		case R.id.nums_zero:
			addDialNum('0');
			break;
		case R.id.nums_star:// 确认按钮
			if (hidden_et.getText().toString().length() == 4) {
				SharedPreferences preferences = this.getSharedPreferences(
						AppConfig.LOGIN_CONFIG, Context.MODE_PRIVATE);
				Editor ed = preferences.edit();
				ed.putString(Contact.LOCAL_LOGIN_PWD, hidden_et.getText()
						.toString());
				ed.commit();
				String msg;
				if (msg_modifypwd.getVisibility() == View.VISIBLE) {
					msg = "更改本机密码成功！";
				} else {
					msg = "本机密码设置成功！";
				}
				if(name.equals("")){
                    //finish();
                }else{
                    Intent mainIntent = new Intent(ctx, MainActivity.class);
                    ctx.startActivity(mainIntent);
                    finish();
                }
				Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(this, "设置本机密码必须为4位！", Toast.LENGTH_SHORT).show();
			}

			break;
		case R.id.nums_no:// 删除按钮
			deleteNum();
			break;
		case R.id.msg_modifypwd:// 点击修改密码
			set_ll.setVisibility(View.VISIBLE);
			break;
		}
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN
				&& event.getKeyCode() == KeyEvent.KEYCODE_BACK
				&& event.getRepeatCount() == 0) {
			SharedPreferences preferences = SystemSetupActivity.this
					.getSharedPreferences(AppConfig.LOGIN_CONFIG,
							Context.MODE_PRIVATE);
			String setuppwdstr = preferences.getString(Contact.LOCAL_LOGIN_PWD,
					"");
			if (rb_setup_yes.isChecked() && setuppwdstr.length() < 4) {
				Toast.makeText(SystemSetupActivity.this, "请设置4位本机密码，并按下设置按钮！",
						Toast.LENGTH_SHORT).show();
			} else {
				onBackPressed();
			}
			return true;
		}
		return super.dispatchKeyEvent(event);
	}

}
